﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar.Metro.ColorTables;
using DAL;
using BLL;
using BOL;

namespace QuanLyHangXeKhach
{
    public partial class frmMain : MetroAppForm   
    {
        frmM frmm = new frmM();
        DieuKhien DK = null;
        frmKhachHangTraCuu frmKHtracuu = null;
        frmTraCuuDatVe frmtracuudatve = null;
        frmThongTinTuyenDuong frmthontintuyendg = null;
        frmDangnhap frmdangnhap = null;
        frmChucNangNhanVien1 frmcnnv = null;        
        string taikhoandangnhap;
        frmThongTinNV frmttnv = null;
        frmCapNhatNoiDen frmcnnoiden = null;
        frmCapNhatNoiDi frmcnndi = null;
        frmCapNhatDiemDungChan frmcnddc = null;
        frmCapNhatTuyenDuong frmcntd = null;
        frmCapNhatGiaTuyen frmcngt = null;
        frmcnnhanvienphutrachxe frmcnnvptxe = null;
        frmCapNhatXe frmcnxe = null;
        frmCapNhatLichChay frmcnlc = null;
        frmCapnhatKhachHang frmcnkh = null;
        frmCapNhatNhanVien frmcnnv1 = null;
        frmBanVe frmbv = null;
        frmCapNhatTaiKhoan frmcaptk = null;
        frmThongKeHoatDong frmtk = null;

        public frmMain()
        {
            DK = new DieuKhien();
            InitializeComponent();
           
            //phân quyền
            DK.NotQuanLy = new Command(components, new EventHandler(phanquyennv));
            //event cua nút đổi giao diện
            DK.DoiGiaoDien = new Command(components, new EventHandler(ThayDoiGiaoDien));

            DK.TraCuuDatVe = new Command(components, new EventHandler(showfrmtracuudatve));
            DK.dongfrmdatve = new Command(components, new EventHandler(dongfrmtracuudatve));

            DK.ThongTinTuyenDuong = new Command(components, new EventHandler(showfrmthongtintuyenduong));
            DK.dongfrmtttuyendg = new Command(components, new EventHandler(dongfrmthongtintuyendg));

            DK.DangNhap = new Command(components, new EventHandler(showfrmdangnhap));
            DK.dongfrmdangnhap = new Command(components, new EventHandler(dongfrmdangnhap));

            DK.showthongtin = new Command(components, new EventHandler(showfrmthongtin));
            DK.dongfrmttnv = new Command(components, new EventHandler(dongfrmttnv));

            DK.dangxuat = new Command(components, new EventHandler(dangxuatnv));

            DK.shownoiden = new Command(components, new EventHandler(showfrmcnnoiden));
            DK.dongfrmcnnd = new Command(components, new EventHandler(dongfrmcnnd));

            DK.shownoidi = new Command(components, new EventHandler(showfrmcnnoidi));
            DK.dongfrmcnndi = new Command(components, new EventHandler(dongfrmcnndi));

            DK.showdiemdung = new Command(components, new EventHandler(showfrmcndiemdung));
            DK.dongfrmcndiemdung = new Command(components, new EventHandler(dongfrmcndiemdung));

            DK.showcntuyenduong = new Command(components, new EventHandler(showfrmcntuyenduong));
            DK.dongfrmcntuyenduong = new Command(components, new EventHandler(dongfrmcntuyenduong));

            DK.showcngiatuyen = new Command(components, new EventHandler(showfrmcngiatuyen));
            DK.dongfrmcngiatuyen = new Command(components, new EventHandler(dongfrmcngiatuyen));

            DK.showcnnvpt = new Command(components, new EventHandler(showfrmcnnvptxe));
            DK.dongfrmcnnvptx = new Command(components, new EventHandler(dongfrmcnnvptxe));

            DK.showcnxe = new Command(components, new EventHandler(showfrmcnxe));
            DK.dongfrmcnxe = new Command(components, new EventHandler(dongfrmcnxe));

            DK.showcnlichchay = new Command(components, new EventHandler(showfrmcnlc));
            DK.dongfrmcnlc = new Command(components, new EventHandler(dongfrmcnlc));

            DK.showcnkhachhang = new Command(components, new EventHandler(showfrmcnkh));
            DK.dongfrmcnkh = new Command(components, new EventHandler(dongfrmcnkh));

            DK.showcnnhanvien = new Command(components, new EventHandler(showfrmcnnv1));
            DK.dongfrmcnnv = new Command(components, new EventHandler(dongfrmcnnv1));

            DK.showbanve = new Command(components, new EventHandler(showfrmbanve));
            DK.dongfrmbanve = new Command(components, new EventHandler(dongfrmbv));

            DK.showcaptk = new Command(components, new EventHandler(showfrmcaptk));
            DK.dongfrmcaptk = new Command(components, new EventHandler(dongfrmcaptk));

            DK.showthongke = new Command(components, new EventHandler(showfrmtk));
            DK.dongfrmtk = new Command(components, new EventHandler(dongfrmtk));


            //add form tra cứu của khách hàng
            frmKHtracuu = new frmKhachHangTraCuu();
            frmKHtracuu.Commands = DK;
            this.Controls.Add(frmKHtracuu);
            frmKHtracuu.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Right;
            UpdateControlsSizeAndLocation(frmKHtracuu);
            

            //add theme metro vào button "thay đổi giao diện"
            MetroColorGeneratorParameters[] metroThemes = MetroColorGeneratorParameters.GetAllPredefinedThemes();
            foreach (MetroColorGeneratorParameters mt in metroThemes)
            {
                ButtonItem theme = new ButtonItem(mt.ThemeName, mt.ThemeName);
                theme.Command = DK.DoiGiaoDien;
                theme.CommandParameter = mt;
                btnTheme.SubItems.Add(theme);
            }

        }

        private void ThayDoiGiaoDien(object sender, EventArgs e)
        {
            ICommandSource source = (ICommandSource)sender;
            MetroColorGeneratorParameters theme = (MetroColorGeneratorParameters)source.CommandParameter;
            StyleManager.MetroColorGeneratorParameters = theme;
        }

        private void metroShell1_HelpButtonClick(object sender, EventArgs e)
        {
            MessageBoxEx.Show("Giúp ... giúp ... cái *Beep*");           
        }

      
        #region  form tra cứu của khách hàng - form chức năng của nhân viên hiện trên form main

        private void showfrmkhachhangtracuu()
        {            
            frmKHtracuu = new frmKhachHangTraCuu();
            frmKHtracuu.Commands = DK;
            this.Controls.Add(frmKHtracuu);            
            frmKHtracuu.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Right;
            UpdateControlsSizeAndLocation(frmKHtracuu);
        }

        private void showfrmchucnangnhanvien()
        {
            frmcnnv = new frmChucNangNhanVien1();
            frmcnnv.tentaikhoan = taikhoandangnhap; 
            frmcnnv.Commands = DK;
            this.Controls.Add(frmcnnv);            
            frmcnnv.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Right;
            frmcnnv.Click += new EventHandler(frmcnnv_Click);                
            UpdateControlsSizeAndLocation(frmcnnv);
        }      


        private void UpdateControlsSizeAndLocation(DevComponents.DotNetBar.Controls.SlidePanel frm)
        {
            int captionHeight = metroShell1.MetroTabStrip.GetCaptionHeight() + 2;
            Thickness borderThickness = this.GetBorderThickness();
            if (frm != null)
            {
                if (!frm.IsOpen)
                    frm.OpenBounds = new Rectangle((int)borderThickness.Left, captionHeight, this.Width - (int)borderThickness.Horizontal, this.Height - captionHeight - 1);
                else
                    frm.Bounds = new Rectangle((int)borderThickness.Left, captionHeight, this.Width - (int)borderThickness.Horizontal, this.Height - captionHeight - 1);
                if (!IsModalPanelDisplayed)
                    frm.BringToFront();
            }

        }
        private void frmMain_Resize(object sender, EventArgs e)
        {
            
        }
        #endregion


        #region form tra cứu đặt vé thông qua mã vé
        void showfrmtracuudatve(object sender, EventArgs e)
        {
            frmtracuudatve = new frmTraCuuDatVe();
            frmtracuudatve.Commands = DK;
            this.ShowModalPanel(frmtracuudatve, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        private void dongfrmtracuudatve(object sender, EventArgs e)
        {
            
            this.CloseModalPanel(frmtracuudatve, DevComponents.DotNetBar.Controls.eSlideSide.Left);           
            frmtracuudatve.Commands = null;
            frmtracuudatve.Dispose();
            frmtracuudatve = null;
        }
        #endregion

        #region form thông tin tuyến đường (tra cứu)
        void showfrmthongtintuyenduong(object sender, EventArgs e)
        {
            frmthontintuyendg = new frmThongTinTuyenDuong();
            frmthontintuyendg.Commands = DK;
            this.ShowModalPanel(frmthontintuyendg, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        private void dongfrmthongtintuyendg(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmthontintuyendg, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmthontintuyendg.Commands = null;
            frmthontintuyendg.Dispose();
            frmthontintuyendg = null;
        }
        #endregion

        #region Form đăng nhập
        void showfrmdangnhap(object sender, EventArgs e)
        {
            frmdangnhap = new frmDangnhap();           
            frmdangnhap.Commands = DK;
            this.ShowModalPanel(frmdangnhap, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmdangnhap.dong = new frmDangnhap.closeDangNhap(dangnhapoke);
            frmdangnhap.btnOk.Focus();
            this.AcceptButton = frmdangnhap.btnOk;
        }

        private void dongfrmdangnhap(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmdangnhap, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmdangnhap.Commands = null;
            frmdangnhap.Dispose();
            frmdangnhap = null;
        }

        //sau khi đăng nhập thành công: đóng frmdangnhap + frmkhachhangtracuu, hiện frmchucnangnhanvien
        public void dangnhapoke(string taikhoan)
        {
            taikhoandangnhap = taikhoan;

            this.Controls.Remove(frmKHtracuu);
            frmKHtracuu = null;

            this.CloseModalPanel(frmdangnhap, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmdangnhap.Commands = null;
            frmdangnhap.Dispose();
            frmdangnhap = null;
            
            showfrmchucnangnhanvien();
                  
        }
        #endregion

        #region form Chức năng của nhân viên
        //thu nhỏ form chức năng nhân viên
        void frmcnnv_Click(object sender, EventArgs e)
        {
            frmcnnv.IsOpen = false;
        }

        void showfrmthongtin(object sender, EventArgs e)
        {
            frmttnv = new frmThongTinNV();
            frmttnv.capnhat = new frmThongTinNV.updatenv(thucthi);
            frmttnv.Commands = DK;
            frmttnv.taikhoan = taikhoandangnhap;
            this.ShowModalPanel(frmttnv, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void dangxuatnv(object sender, EventArgs e)
        {
            DialogResult dr = MessageBoxEx.Show("Bạn muốn đăng xuất khỏi chương trình?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                this.Controls.Remove(frmcnnv);
                frmcnnv = null;
                showfrmkhachhangtracuu();
            }           
        }

        void showfrmcnnoiden(object sender, EventArgs e)
        {
            frmcnnoiden = new frmCapNhatNoiDen();            
            frmcnnoiden.Commands = DK;           
            this.ShowModalPanel(frmcnnoiden, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcnnoidi(object sender, EventArgs e)
        {
            frmcnndi = new frmCapNhatNoiDi();
            frmcnndi.Commands = DK;
            this.ShowModalPanel(frmcnndi, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcndiemdung(object sender, EventArgs e)
        {
            frmcnddc = new frmCapNhatDiemDungChan();
            frmcnddc.Commands = DK;
            this.ShowModalPanel(frmcnddc, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcntuyenduong(object sender, EventArgs e)
        {
            frmcntd = new frmCapNhatTuyenDuong();
            frmcntd.Commands = DK;
            this.ShowModalPanel(frmcntd, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcngiatuyen(object sender, EventArgs e)
        {
            frmcngt = new frmCapNhatGiaTuyen();
            frmcngt.Commands = DK;
            this.ShowModalPanel(frmcngt, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcnnvptxe(object sender, EventArgs e)
        {
            frmcnnvptxe = new frmcnnhanvienphutrachxe();
            frmcnnvptxe.Commands = DK;
            this.ShowModalPanel(frmcnnvptxe, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcnxe(object sender, EventArgs e)
        {
            frmcnxe = new frmCapNhatXe();
            frmcnxe.Commands = DK;
            this.ShowModalPanel(frmcnxe, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcnlc(object sender, EventArgs e)
        {
            frmcnlc = new frmCapNhatLichChay();
            frmcnlc.Commands = DK;
            this.ShowModalPanel(frmcnlc, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcnkh(object sender, EventArgs e)
        {
            frmcnkh = new frmCapnhatKhachHang();
            frmcnkh.Commands = DK;
            this.ShowModalPanel(frmcnkh, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcnnv1(object sender, EventArgs e)
        {
            frmcnnv1 = new frmCapNhatNhanVien();
            frmcnnv1.Commands = DK;
            this.ShowModalPanel(frmcnnv1, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmbanve(object sender, EventArgs e)
        {
            frmbv = new frmBanVe();
            frmbv.Commands = DK;
            frmbv.taikhoan = taikhoandangnhap;
            this.ShowModalPanel(frmbv, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmcaptk(object sender, EventArgs e)
        {
            frmcaptk = new frmCapNhatTaiKhoan();
            frmcaptk.Commands = DK;            
            this.ShowModalPanel(frmcaptk, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }

        void showfrmtk(object sender, EventArgs e)
        {
            frmtk = new frmThongKeHoatDong();
            frmtk.Commands = DK;
            this.ShowModalPanel(frmtk, DevComponents.DotNetBar.Controls.eSlideSide.Left);
        }
        #endregion

        #region form thông tin nhân viên
        //hàm update thông tin nv sử dung delegate ở frm cập nhật
        public void thucthi()
        {
            frmcnnv.Loadthongtinnv();
        }      

        private void dongfrmttnv(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmttnv, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmttnv.Commands = null;
            frmttnv.Dispose();
            frmttnv = null;
        }
        #endregion

        #region form cập nhật nơi đên
        
        private void dongfrmcnnd(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnnoiden, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnnoiden.Commands = null;
            frmcnnoiden.Dispose();
            frmcnnoiden = null;
            if (frmcntd != null)
             frmcntd.loadcbonoiden();
        }
        #endregion

        #region form cập nhật nơi đi

        private void dongfrmcnndi(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnndi, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnndi.Commands = null;
            frmcnndi.Dispose();
            frmcnndi = null;
            if (frmcntd != null)
                frmcntd.loadcbonoidi();
            
        }
        #endregion

        #region form cập nhật điểm dừng chân

        private void dongfrmcndiemdung(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnddc, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnddc.Commands = null;
            frmcnddc.Dispose();
            frmcnddc = null;
            if (frmcntd != null)
             frmcntd.loadcbodiemdung();
        }
        #endregion

        #region form cập nhật tuyến đường

        private void dongfrmcntuyenduong(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcntd, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcntd.Commands = null;
            frmcntd.Dispose();
            frmcntd = null;
        }
        #endregion

        #region form cập nhật giá tuyến đường

        private void dongfrmcngiatuyen(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcngt, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcngt.Commands = null;
            frmcngt.Dispose();
            frmcngt = null;
        }
        #endregion

        #region form cập nhật nhân viên phụ trách xe
        private void dongfrmcnnvptxe(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnnvptxe, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnnvptxe.Commands = null;
            frmcnnvptxe.Dispose();
            frmcnnvptxe = null;
        }
        #endregion

        #region form cập nhật xe
        private void dongfrmcnxe(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnxe, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnxe.Commands = null;
            frmcnxe.Dispose();
            frmcnxe = null;
        }
        #endregion

        #region form cập nhật lịch chạy
        private void dongfrmcnlc(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnlc, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnlc.Commands = null;
            frmcnlc.Dispose();
            frmcnlc = null;
        }
        #endregion

        #region form cập nhật khách hàng
        private void dongfrmcnkh(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnkh, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnkh.Commands = null;
            frmcnkh.Dispose();
            frmcnkh = null;
        }
        #endregion

        #region form bán vé
        private void dongfrmbv(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmbv, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmbv.Commands = null;
            frmbv.Dispose();
            frmbv = null;
        }
        #endregion

        #region form cập nhật nhân viên
        private void dongfrmcnnv1(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcnnv1, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcnnv1.Commands = null;
            frmcnnv1.Dispose();
            frmcnnv1 = null;
        }
        #endregion

        #region form cập nhật nhân viên
        private void dongfrmcaptk(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmcaptk, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmcaptk.Commands = null;
            frmcaptk.Dispose();
            frmcaptk = null;
        }
        #endregion

        #region form thống kê
        private void dongfrmtk(object sender, EventArgs e)
        {

            this.CloseModalPanel(frmtk, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            frmtk.Commands = null;
            frmtk.Dispose();
            frmtk = null;
        }
        #endregion

        private void frmMain_Resize_1(object sender, EventArgs e)
        {
            if (frmKHtracuu != null)
            UpdateControlsSizeAndLocation(frmKHtracuu);
            else UpdateControlsSizeAndLocation(frmcnnv);
        }

        #region Phân quyền
        private void phanquyennv(object sender, EventArgs e)
        {

            MessageBoxEx.Show("Chức năng dành riêng cho người quản lý", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        private void metroShell1_Click(object sender, EventArgs e)
        {

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            
        }
    }
}
